package com.aiwei.tdjk.bean;

import com.wordnik.swagger.annotations.ApiModel;
import com.wordnik.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanfeng on 15/12/30.
 */
@ApiModel(value = "ApiResult",description = "接口返回的对象")
public class ApiResult<TData> {


    @ApiModelProperty(value = "错误码")
    public int errorCode ;

    @Override
	public String toString() {
		return "ApiResult [errorCode=" + errorCode + ", errorMessage=" + errorMessage + ", clientError=" + clientError
				+ ", firstError=" + firstError + ", debugError=" + debugError + ", success=" + success
				+ ", clientSuccess=" + clientSuccess + ", data=" + data + "]";
	}


    @ApiModelProperty(value = "出错消息列表")
    public List<String> errorMessage;

    @ApiModelProperty(value = "客户端应显示的错误信息")
    public String clientError;


    @ApiModelProperty(value = "首条出错信息")
    public String firstError;


    @ApiModelProperty(value = "内部出错信息（不用于页面显示）")
    public String debugError;


    @ApiModelProperty(value = "调用接口是否成功")
    public Boolean success;

    @ApiModelProperty(value = "客户端成功")
    public Boolean clientSuccess;

    @ApiModelProperty(value = "返回的数据")
    public TData data;


    public String getFirstError() {
        if(errorMessage==null || errorMessage.isEmpty()) return "";
        return this.errorMessage.get(0);
    }

    public void setFirstError(List<String> errorMessage) {
        this.firstError = errorMessage.get(0);
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public String getDebugError() {
        return debugError;
    }

    public void setDebugError(String debugError) {
        this.debugError = debugError;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public List<String> getErrorMessage() {

        return errorMessage;
    }

    public void setErrorMessage(List<String> errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getClientError() {
        return clientError;
    }

    public void setClientError(String clientError) {
        this.clientError = clientError;
    }

    public static ApiResult makeSucessResult()
    {
        ApiResult result = new ApiResult();
        result.setErrorCode(0);
        result.setSuccess(true);
        return result;
    }

    public static ApiResult makeFailedResult()
    {
        ApiResult result = new ApiResult();
        result.setSuccess(false);
        result.setErrorCode(-1);
        return result;
    }

    public TData getData() {
        return data;
    }

    public void setData(TData data) {
        this.data = data;
    }

    public Boolean getClientSuccess() {
        return clientSuccess;
    }

    public void setClientSuccess(Boolean clientSuccess) {
        this.clientSuccess = clientSuccess;
    }

    /*

        /// <summary>
        /// CToken或UToken是否有误
        /// </summary>
        public bool HasWrongToken
        {
            get
            {
                return ErrorCode == (int)ContextError.MissCToken || ErrorCode == (int)ContextError.MissUToken ||
                        ErrorCode == (int)ContextError.WrongCToken || ErrorCode == (int)ContextError.WrongCUToken;
            }
        }

        */
    //ToDo 默认的错误码是-1，应该传递过来错误码
    public void failed(String error)
    {
        this.setSuccess(false);
        this.firstError = error;
        List<String> list = new ArrayList<String>();
        list.add(error);
        this.errorMessage=list;
        errorCode = -1;
    }

    public void debugFailed(String error)
    {
        this.setSuccess(false);
        debugError = error;
        errorCode = -1;
    }

    public void failed(String error, String debugError)
    {
        this.setSuccess(false);
        this.firstError = error;
        this.debugError = debugError;
        List<String> list = new ArrayList<String>();
        list.add(error);
        this.errorMessage=list;
        errorCode = -1;
    }

    /**
     * 接口出错时
     * @param errMsg
     * @return
     */
    public static ApiResult makeServerFailed(String errMsg){
        ApiResult result = makeFailedResult();

        result.failed("服务器错误:"+errMsg);
        return result;
    }

    /**
     * 全部正确时
     * @return
     */
    public static ApiResult  makeClientSuccess(){
        ApiResult result = new ApiResult();
        result.setErrorCode(0);
        result.setSuccess(true);
        result.setClientSuccess(true);
        return result;
    }

    /**
     * 接口正确，但客户端需要返回错误信息时
     * @param clientError
     * @return
     */
    public static ApiResult makeClientFailed(String clientError){
        ApiResult result = new ApiResult();
        result.setErrorCode(-1);
        result.setSuccess(true);
        result.setClientSuccess(false);
        result.setClientError(clientError);
        return result;
    }

}
